package com.cn.onetrip.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.NetworkManager;
import com.cn.onetrip.untility.SysApplication;

public class MoreWebActivity extends Activity {

	private WebView webView;
	private RelativeLayout layoutProgress = null;
	private ImageView titleImage;
	private String webUrl = "";
	private String webName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (SysApplication.routesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_more_web);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		webUrl = bundle.getString("webUrl");
		webName = bundle.getString("webName");

		titleImage = (ImageView) findViewById(R.id.content_title);
		if (webName.equals("feedback")) {
			titleImage.setBackgroundResource(R.drawable.more_feedback_title);
		} else if (webName.equals("state")) {
			titleImage.setBackgroundResource(R.drawable.more_protocol_title);
		} else if (webName.equals("App")) {
			titleImage.setBackgroundResource(R.drawable.more_appdownload_title);
		}

		Button web_return = (Button) findViewById(R.id.quit);
		web_return.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
				System.gc();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});

		webView = (WebView) findViewById(R.id.webView);
		if (!NetworkManager.CheckNetworkState(getApplicationContext())) {
			webView.getSettings().setJavaScriptEnabled(true);
			if (webName.equals("state")) {
				webView.loadUrl(" file:///android_asset/Introduction.htm ");
			}
		} else {
			layoutProgress = (RelativeLayout) findViewById(R.id.layout_progress);
			loadWeb();
		}
	}

	private void loadWeb() {
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl(webUrl);
		layoutProgress.setVisibility(View.VISIBLE);
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub

				layoutProgress.setVisibility(View.GONE);
				super.onPageFinished(view, url);

			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
			}

		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

}
